package com.inspinia.base.api.service;

import com.inspinia.base.api.dao.InterfaceUserDao;
import com.inspinia.base.api.model.ApiException;
import com.inspinia.base.api.model.InterfaceUser;
import com.inspinia.base.api.model.InterfaceUserDataAuthority;
import com.inspinia.base.api.model.InterfaceUserExternal;
import com.inspinia.base.common.exception.ApiErrorCode;
import com.inspinia.base.common.model.KeyParameters;
import com.inspinia.base.dao.BaseDao;
import com.inspinia.base.service.BaseServiceImpl;
import com.inspinia.base.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 的服务层实现类
 * Created by pujw on 2017-6-30.
 */
@Service
public class InterfaceUserServiceImpl extends BaseServiceImpl<InterfaceUser> implements InterfaceUserService {
    @Resource
    private InterfaceUserDao interfaceUserDao;
    @Resource
    private ExternalInterfaceService externalInterfaceService;
    @Resource
    private InterfaceUserExternalService interfaceUserExternalService;
    @Resource
    private InterfaceUserDataAuthorityService interfaceUserDataAuthorityService;

    @Override
    protected BaseDao<InterfaceUser> getDao() {
        return interfaceUserDao;
    }

    @Transactional
    @Override
    public int batchDelete(Long[] ids) {
        return interfaceUserDao.batchDelete(ids);
    }

    @Override
    public Long userValidate(KeyParameters keyParameters) {
        Long provinceId = keyParameters.getCalleeProvinceId();
        Long cityId = keyParameters.getCalleeCityId();
        InterfaceUser interfaceUser = interfaceUserDao.findByUserName(keyParameters.getUserName());

        //如果用户不存在
        if (interfaceUser == null) {
            throw new ApiException(ApiErrorCode.userNotExist);
        }
        //判断用户是否启用
        if (!interfaceUser.isEnable()) {
            throw new ApiException(ApiErrorCode.userStatusException);
        }
        //判断省id是否为空
        if (provinceId != null) {
            //如果省id不为空则直接使用省id去查询
            InterfaceUserDataAuthority interfaceUserDataAuthority = interfaceUserDataAuthorityService.findByProvinceId(Long.valueOf(provinceId), interfaceUser.getId());
            if (interfaceUserDataAuthority != null) {
                return interfaceUser.getId();
            }
        }
        if (cityId != null) {
            InterfaceUserDataAuthority interfaceUserDataAuthority = interfaceUserDataAuthorityService.findByCityId(Long.valueOf(cityId), interfaceUser.getId());
            if (interfaceUserDataAuthority != null) {
                return interfaceUser.getId();
            }
        }
        throw new ApiException(ApiErrorCode.userNoDataAuthority);
    }

    @Override
    public List<String> findUrlsByUserName(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }
        InterfaceUser user = interfaceUserDao.findByUserName(username);
        List<InterfaceUserExternal> interfaces = interfaceUserExternalService.findAddrsByUserId(user.getId());
        List<String> addrs = new ArrayList<>();
        interfaces.forEach(interfaceVo -> {
            addrs.add(externalInterfaceService.get(interfaceVo.getExternalInterfaceId()).getAddress());
        });
        return addrs;
    }
}
